﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.Entity;
using System.Data.EntityClient;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Microsoft.Practices.Unity;
using MiniStock.Database;
using MiniStock.Entities.Common;
using MiniStock.Entities.Stock;
using MiniStock.Infrastructure.EFExtensions;
using MiniStock.Entities.Common.Query;

namespace MiniStock.BusinessLogic.QueryLogic.Stock
{
    public class StockQueryLogic
    {
        [Dependency]
        public EntityDbContext Context { get; set; }


        public Customer GetCustomer(Guid id)
        {
            var result = Context.Customers
                .Include("Orders")
                .FirstOrDefault(i => i.Id == id);
            return result;
        }

        public List<Customer> ListCustomers(List<FilterElement> filter, string order, int? pageSize, int? pageIndex)
        {
            var builder = new SqlQueryBuilder(Context)
                              {
                                  EntityType = typeof (Customer),
                                  Filter = filter,
                                  Order = order,
                                  PageSize = pageSize,
                                  PageIndex = pageIndex,
                              };
            var query = builder.ToSql();
            var cmd = Context.Database.Connection.CreateCommand();
            cmd.CommandText = query;

            return cmd.MapResults<Customer>();
        }

        public Product GetProduct(Guid id)
        {
            var result = Context.Products
                .Include("Manufacturer")
                .Include("OrderItems")
                .FirstOrDefault(i => i.Id == id);
            return result;
        }

        public List<Product> ListProducts(List<FilterElement> filter, string order, int? pageSize, int? pageIndex)
        {
            var builder = new SqlQueryBuilder(Context)
            {
                EntityType = typeof(Product),
                Filter = filter,
                Order = order,
                PageSize = pageSize,
                PageIndex = pageIndex,
            };
            var query = builder.ToSql();
            var cmd = Context.Database.Connection.CreateCommand();
            cmd.CommandText = query;

            return cmd.MapResults<Product>();
        }

        public Order GetOrder(Guid id)
        {
            var result = Context.Orders
                .Include("Items")
                .Include("Items.Product")
                .Include("Customer")
                .FirstOrDefault(i => i.Id == id);
            return result;
        }

        public List<Order> ListOrders(List<FilterElement> filter, string order, int? pageSize, int? pageIndex)
        {
            var builder = new SqlQueryBuilder(Context)
                              {
                                  EntityType = typeof (Order),
                                  Filter = filter,
                                  Order = order,
                                  PageSize = pageSize,
                                  PageIndex = pageIndex,
                                  Joins = new Dictionary<Type, string>
                                              {
                                                  {typeof (Customer), null}
                                              }
                              };
            var query = builder.ToSql();
            var cmd = Context.Database.Connection.CreateCommand();
            cmd.CommandText = query;

            return cmd.MapResults<Order>();
        }

    }
}
